package com.ruoyi.customerService.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.InitTableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.customerService.domain.CsClaimsBillingRecords;
import com.ruoyi.customerService.domain.CsClaimsBillingRecordsCollect;
import com.ruoyi.customerService.domain.SheetType;
import com.ruoyi.customerService.domain.vo.CsClaimsBillingRecordsVo;
import com.ruoyi.customerService.mapper.CsClaimsBillingRecordsMapper;
import com.ruoyi.customerService.service.CsClaimsBillingRecordsService;
import com.ruoyi.customerService.service.SheetTypeService;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.logistic.domain.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cs/claimsBill")
@Api(tags = "客服中心-理赔账单")
public class CsClaimsBillingRecordsController {
    @Autowired
    private CsClaimsBillingRecordsService csClaimsBillingRecordsService;
    @Autowired
    private TableService tableService;

    @ApiOperation("查全部")
    @GetMapping("/selectAll")
    public R selectAll(PageDate pageDate) {
        List<CsClaimsBillingRecords> sheetTypes = csClaimsBillingRecordsService.selectAll(pageDate);
        if (sheetTypes == null || sheetTypes.size() == 0) {
            return R.fail("暂无数据");
        } else {
            return R.ok(PageInfo.info(sheetTypes));
        }
    }

    @ApiOperation("添加")
    @PostMapping("/insert")
    public R insert(@RequestBody CsClaimsBillingRecords csClaimsBillingRecords) {
        Boolean isAdd = csClaimsBillingRecordsService.insert(csClaimsBillingRecords);
        if (isAdd) {
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R update(@RequestBody CsClaimsBillingRecords csClaimsBillingRecords) {
        Boolean isUpdate = csClaimsBillingRecordsService.update(csClaimsBillingRecords);
        if (isUpdate) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public R delete(Integer id) {
        Boolean isDelete = csClaimsBillingRecordsService.deleteById(id);
        if (isDelete) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @ApiOperation("查询")
    @PostMapping("/search")
    public R search(@RequestBody CsClaimsBillingRecordsVo vo) {
        if (vo.getStartTime() == null || vo.getEndTime() == null) {
            return R.fail("请输入查询所需条件");
        }
        List<CsClaimsBillingRecords> sheetTypes = csClaimsBillingRecordsService.search(vo);
        if (sheetTypes == null || sheetTypes.size() == 0) {
            return R.fail("暂无数据");
        } else {
            JSONObject jsonObject = new JSONObject();
            String[] fields = {
                    "transportCode", "pickupScanNetwork",
                    "claimType", "billGenerationDate",
                    "againstMeComplaintAmount", "complaintCompensationAmount",
                    "violationServiceFee", "arbitrationFailureEvidenceFee",
                    "negotiatedSettlementAmount", "handlingResult",
                    "complainantPartyName", "complainantCustodyServiceBranchName",
                    "defendantParty1Name", "defendantParty1CustodyServiceBranchName",
                    "defendantParty2Name", "defendantParty2CustodyServiceBranchName"
            };
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("csClaimsBillingRecordsDetail");
            jsonObject.put("list",PageInfo.info(sheetTypes));
            jsonObject.put("fields", tableService.initTable(initTableVo));
            return R.ok(jsonObject);
        }
    }

    @ApiOperation("汇总")
    @PostMapping("/collect")
    public R collect(@RequestBody CsClaimsBillingRecordsVo vo) {
        if (vo.getStartTime() == null || vo.getEndTime() == null) {
            return R.fail("请输入查询所需条件");
        }
        List<CsClaimsBillingRecordsCollect> sheetTypes = csClaimsBillingRecordsService.collect(vo);
        if (sheetTypes == null || sheetTypes.size() == 0) {
            return R.fail("暂无数据");
        } else {
            JSONObject jsonObject = new JSONObject();
            String[] fields = {"myComplaintAmount", "myComplaintVolume", "againstMeComplaintAmount", "againstMeComplaintVolume"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("csClaimsBillingRecordsCollect");
            jsonObject.put("list",PageInfo.info(sheetTypes));
            jsonObject.put("fields", tableService.initTable(initTableVo));
            return R.ok(jsonObject);
        }
    }

}
